package com.kikoulol.jarmoryextactor;

import java.util.ArrayList;
import java.util.List;

import noNamespace.ArenaLadderPagedResult;
import noNamespace.ArenaTeam;
import noNamespace.PageDocument;

import org.apache.xmlbeans.XmlException;

import com.kikoulol.jarmoryextactor.utils.Utils;

public class Ladder {

	public static final String TWO = "2";
	public static final String THREE = "3";
	public static final String FIVE = "5";
	 

	private String type;
	private String armoryUrl;
	private String battleGroupName;
	private String ladderUrl;
	private int teamPerView = 20; // default wow armory value


	public Ladder(String type,String armoryUrl, String battleGroupName) throws Exception {
		this.type = type;
		this.armoryUrl = armoryUrl;
		this.battleGroupName = battleGroupName;
		ladderUrl = armoryUrl + "arena-ladder.xml?ts=" + type + "&b=" + battleGroupName;
	}
	
	public ArenaLadderPagedResult getLadderPage(String type,String armoryUrl, String battleGroupName,int numPage) throws XmlException, InterruptedException {
		String url = armoryUrl + "arena-ladder.xml?p=" + numPage + "&ts=" + type + "&b=" + battleGroupName;
		String xml = null;
		xml = Utils.getArmoryXml(url);
		PageDocument ladderDocument = PageDocument.Factory.parse(xml);
		ArenaLadderPagedResult myLadder =  ladderDocument.getPage().getArenaLadderPagedResult();
		return myLadder;
	}

	public String getLadderXml() throws InterruptedException {
		String xml = null;
		xml = Utils.getArmoryXml(ladderUrl);
		return xml;
	}
	
	public List<Team> getTeams(long numberOfTeams) throws XmlException, InterruptedException {
		
		// First pass to get the maxPage number 
		ArenaLadderPagedResult ladder = getLadderPage(type, armoryUrl, battleGroupName, 1);
		int maxPage = Integer.parseInt(ladder.getMaxPage());
		
		List<Team> returnList = new ArrayList<Team>();
		int i = 1; // ladder page number
		while (returnList.size() < numberOfTeams  && i <= maxPage) {
			
			// Load the ladder Page
			ladder = getLadderPage(type, armoryUrl, battleGroupName, i);
			// Add the actuel list of team to the memory
			ArenaTeam[] teams = ladder.getArenaTeams().getArenaTeamArray();
			for (ArenaTeam team : teams) {
				Team myTeam = new Team(armoryUrl, team.getUrl());
				returnList.add(myTeam);	
				if(returnList.size() == numberOfTeams) {
					break;
				}
			}
			i++; // go to the next armory ladder page
			ladder = getLadderPage(type, armoryUrl, battleGroupName,i);
		}
		return returnList;
	}
	
	public List<Team> getGladTeam() throws XmlException, InterruptedException {
		
		ArenaLadderPagedResult ladder = getLadderPage(type, armoryUrl, battleGroupName, getMaxPage());
		ArenaTeam[] teams = ladder.getArenaTeams().getArenaTeamArray();
		ArenaTeam lastTeam = teams[teams.length - 1];
		
		double gladNum = (5.0/100.0) * Double.parseDouble(lastTeam.getRanking());
		String gladNumber = "" + gladNum;
		int test = Integer.parseInt(gladNumber.substring(0, gladNumber.indexOf(".")));
		
		return getTeams(test);
	}
	
	public List<Team> getTeams() throws XmlException, InterruptedException {
		return getTeams(getMaxPage());
	}

	public int getMaxPage() throws XmlException, InterruptedException {
		ArenaLadderPagedResult ladder = getLadderPage(type, armoryUrl, battleGroupName, 1);
		return Integer.parseInt(ladder.getMaxPage());
	}

	public int getTeamPerView() {
		return teamPerView;
	}

	public void setTeamPerView(int teamPerView) {
		this.teamPerView = teamPerView;
	}

}
